package com.neuedu;

import java.sql.*;

public class HiveToMySQLC {
    public static void main(String[] args) {
        String hiveJdbcUrl = "jdbc:hive2://192.168.47.101:10000/vehicledb"; // Hive JDBC URL
        String mysqlJdbcUrl = "jdbc:mysql://192.168.47.101:3306/vehicledb"; // MySQL JDBC URL
        String mysqlUser = "root";
        String mysqlPassword = "123456";

        Connection hiveConn = null;
        Connection mysqlConn = null;
        Statement hiveStmt = null;
        Statement mysqlStmt = null;
        ResultSet rs = null;

        try {
            // 1. 连接到 Hive
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            hiveConn = DriverManager.getConnection(hiveJdbcUrl, "", "");
            hiveStmt = hiveConn.createStatement();

            // 2. 从 Hive 查询数据
            String hiveQuery = "SELECT times, nev_production, nev_sales, bev_production, bev_sales, phev_production, phev_sales FROM c_vehicle_production_sales";
            rs = hiveStmt.executeQuery(hiveQuery);

            // 3. 连接到 MySQL
            Class.forName("com.mysql.cj.jdbc.Driver");
            mysqlConn = DriverManager.getConnection(mysqlJdbcUrl, mysqlUser, mysqlPassword);
            mysqlStmt = mysqlConn.createStatement();

            // 4. 创建 MySQL 插入语句
            String insertQuery = "INSERT INTO c_vehicle_production_sales (times, nev_production, nev_sales, bev_production, bev_sales, phev_production, phev_sales) VALUES (?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement mysqlStmtPrepared = mysqlConn.prepareStatement(insertQuery);

            // 5. 将数据从 Hive 导入 MySQL
            while (rs.next()) {
                Date times = rs.getDate("times");
                int nevProduction = rs.getInt("nev_production");
                int nevSales = rs.getInt("nev_sales");
                int bevProduction = rs.getInt("bev_production");
                int bevSales = rs.getInt("bev_sales");
                int phevProduction = rs.getInt("phev_production");
                int phevSales = rs.getInt("phev_sales");

                // 设置参数
                mysqlStmtPrepared.setDate(1, times);
                mysqlStmtPrepared.setInt(2, nevProduction);
                mysqlStmtPrepared.setInt(3, nevSales);
                mysqlStmtPrepared.setInt(4, bevProduction);
                mysqlStmtPrepared.setInt(5, bevSales);
                mysqlStmtPrepared.setInt(6, phevProduction);
                mysqlStmtPrepared.setInt(7, phevSales);

                // 执行插入
                mysqlStmtPrepared.executeUpdate();
            }

            System.out.println("Data successfully transferred from Hive to MySQL (c_vehicle_production_sales)");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (hiveStmt != null) hiveStmt.close();
                if (hiveConn != null) hiveConn.close();
                if (mysqlStmt != null) mysqlStmt.close();
                if (mysqlConn != null) mysqlConn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

